package com.ziyueyuwen.xiniao.service.impl;

import com.alibaba.fastjson.JSON;
import com.ziyueyuwen.xiniao.bean.ZyClassLessonInfo;
import com.ziyueyuwen.xiniao.bean.ZyClassLessonUser;
import com.ziyueyuwen.xiniao.bean.vo.ZyClassLessonUserVO;
import com.ziyueyuwen.xiniao.bean.vo.ZyClassUserVO;
import com.ziyueyuwen.xiniao.bean.vo.ZyLessonVO;
import com.ziyueyuwen.xiniao.dao.ZyClassLessonUserMapper;
import com.ziyueyuwen.xiniao.enums.ClassUserTypeEnum;
import com.ziyueyuwen.xiniao.service.ClassUserService;
import com.ziyueyuwen.xiniao.service.LessonUserService;
import com.ziyueyuwen.xiniao.service.UserService;
import com.ziyueyuwen.xiniao.service.baseservice.lesson.ZyClassLessonService;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author wancheng  on 2019/1/22.
 */
@Service
public class LessonUserServiceImpl implements LessonUserService {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    ZyClassLessonUserMapper zyClassLessonUserMapper;
    @Autowired
    UserService userService;
    @Autowired
    ZyClassLessonService zyClassLessonService;
    @Autowired
    ClassUserService classUserService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertBatch(List<ZyClassLessonUser> lessonUsers) {
        zyClassLessonUserMapper.insertList(lessonUsers);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertBatch(Integer classId, String teacherAccount, List<ZyClassLessonInfo> lessonInfos) {
        //保存课节老师的数据
        List<ZyClassLessonUser> lessonUsers = new ArrayList<>();
        //学生和老师相关信息
        List<ZyClassUserVO> classStudent = classUserService.getStudent(classId);
        lessonInfos.forEach(
                l -> {
                    ZyClassLessonUser teacherMsg = new ZyClassLessonUser(l.getId(), teacherAccount, ClassUserTypeEnum.TEACHER, new Date());
                    lessonUsers.add(teacherMsg);
                    classStudent.forEach(
                            s -> {
                                ZyClassLessonUser lessonStudent = new ZyClassLessonUser(l.getId(), s.getUserAccount(), ClassUserTypeEnum.STUDENT, new Date());
                                lessonUsers.add(lessonStudent);
                            }
                    );
                }
        );
        logger.info("课节-老师和学生添加,data={}", JSON.toJSON(lessonUsers));
        //添加课节用户
        this.insertBatch(lessonUsers);
    }

    @Override
    public List<ZyClassLessonUser> getLessonUser(Integer lessonId, ClassUserTypeEnum type) {
        ZyClassLessonUser param = new ZyClassLessonUser();
        param.setClassLessonId(lessonId);
        param.setType(type);
        return zyClassLessonUserMapper.select(param);
    }

    @Override
    public ZyClassLessonUser getLessonUser(ZyClassLessonUser param) {
        return zyClassLessonUserMapper.selectOne(param);
    }


    @Override
    public ZyClassLessonUser getTeacher(Integer lessonId) {
        ZyClassLessonUser param = new ZyClassLessonUser();
        param.setClassLessonId(lessonId);
        param.setType(ClassUserTypeEnum.TEACHER);
        return zyClassLessonUserMapper.selectOne(param);
    }

    @Override
    public void updateTeacher(Integer lessonId, String teacherAccount) {
        ZyClassLessonUser teacher = getTeacher(lessonId);
        if (teacher == null) {
            teacher = new ZyClassLessonUser();
            teacher.setClassLessonId(lessonId);
            teacher.setType(ClassUserTypeEnum.TEACHER);
            teacher.setUserAccount(teacherAccount);
            teacher.setCreateTime(new Date());
            zyClassLessonUserMapper.insertSelective(teacher);
        } else {
            teacher.setUserAccount(teacherAccount);
            zyClassLessonUserMapper.updateByPrimaryKeySelective(teacher);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteTeacher(Integer lessonId) {
        ZyClassLessonUser param = new ZyClassLessonUser();
        param.setClassLessonId(lessonId);
        param.setType(ClassUserTypeEnum.TEACHER);
        zyClassLessonUserMapper.delete(param);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteLessonUser(Integer lessonId) {
        ZyClassLessonUser param = new ZyClassLessonUser();
        param.setClassLessonId(lessonId);
        zyClassLessonUserMapper.delete(param);
    }

    @Override
    public void deleteLessonStudent(Integer lessonId, String studentAccount) {
        ZyClassLessonUser param = new ZyClassLessonUser();
        param.setClassLessonId(lessonId);
        param.setUserAccount(studentAccount);
        param.setType(ClassUserTypeEnum.STUDENT);
        zyClassLessonUserMapper.delete(param);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addLessonStudent(Integer classId, List<String> studentAccount) {
        List<ZyClassLessonInfo> lessonInfos = zyClassLessonService.getLessonByClassId(classId);
        if (CollectionUtils.isEmpty(lessonInfos)) {
            return;
        }
        for (ZyClassLessonInfo l : lessonInfos) {
            for (String account : studentAccount) {
                ZyClassLessonUser lessonUser = new ZyClassLessonUser();
                lessonUser.setClassLessonId(l.getId());
                lessonUser.setType(ClassUserTypeEnum.STUDENT);
                lessonUser.setUserAccount(account);
                if (zyClassLessonUserMapper.selectOne(lessonUser) == null) {
                    lessonUser.setCreateTime(new Date());
                    zyClassLessonUserMapper.insertSelective(lessonUser);
                }
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteClassLessonStudent(Integer classId, List<String> studentAccount) {
        //删除未上课节的相关信息
        List<ZyLessonVO> unUsedLesson = zyClassLessonService.getLessonUnUsed(classId);
        for (ZyLessonVO l : unUsedLesson) {
            for (String account : studentAccount) {
                ZyClassLessonUser lessonUser = new ZyClassLessonUser();
                lessonUser.setUserAccount(account);
                lessonUser.setClassLessonId(l.getId());
                zyClassLessonUserMapper.delete(lessonUser);
            }
        }
    }

    @Override
    public List<ZyClassLessonUserVO> getStudent(Integer lessonId) {
        return zyClassLessonUserMapper.getStudent(lessonId);
    }
}
